package com.sunrise.comunicacao.repositorio;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sunrise.comunicacao.repositorio.iface.ContaSmsDao;
import com.sunrise.generico.acesso.DaoService;
import com.sunrise.generico.entidade.ContaSms;
import com.sunrise.generico.entidade.Usuario;
import com.sunrise.generico.visao.Filtro;

@Repository
public class ContaSmsDaoImpl extends DaoService<ContaSms> implements ContaSmsDao {

	public List<ContaSms> listarOrdenado() {
		List<ContaSms> list = listar(ContaSms.class);
		Collections.sort(list, new Comparator<ContaSms>() {

			@Override
			public int compare(ContaSms o1, ContaSms o2) {
				return o1.getNome().compareTo(o2.getNome());
			}
		});
		return list;
	}

	public List<ContaSms> search(ContaSms ex) {
		List<ContaSms> list = searchByExample(ContaSms.class, ex);
		Collections.sort(list, new Comparator<ContaSms>() {

			@Override
			public int compare(ContaSms o1, ContaSms o2) {
				return o1.getNome().compareTo(o2.getNome());
			}
		});
		return list;
	}

	public ContaSms getById(Long id) {
		return getById(id, ContaSms.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContaSms> listarPorNome(String nome) {
		Criteria crit = getSession().createCriteria(ContaSms.class);
		crit.add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE));
		return crit.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<ContaSms> buscarPorFiltros(Filtro contaSms) {
		Criteria crit = getSession().createCriteria(ContaSms.class);
		if (contaSms.getNome() != null) {
			crit.add(Restrictions.ilike("nome", contaSms.getNome(), MatchMode.ANYWHERE));
		}

		if (contaSms.getAtivo() != null) {
			crit.add(Restrictions.eq("ativo", contaSms.getAtivo().getCodigo()));
		}
		crit.createAlias("usuarios", "usuario");
		crit.add(Restrictions.or(Restrictions.eq("criador.id", contaSms.getIdUsuarioLogado()),
				Restrictions.eq("usuario.id", contaSms.getIdUsuarioLogado())));

		crit.addOrder(Order.asc("nome"));
		return crit.list();
	}

	@Override
	public ContaSms buscarPorNome(String nome) {
		Criteria crit = getSession().createCriteria(ContaSms.class);
		crit.add(Restrictions.eq("nome", nome).ignoreCase());
		return (ContaSms) crit.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContaSms> buscarContaSmsPorNome(String[] nomes) {
		Criteria crit = getSession().createCriteria(ContaSms.class);
		Disjunction or = Restrictions.disjunction();
		crit.add(or);
		for (String nome : nomes) {
			or.add(Restrictions.eq("nome", nome).ignoreCase());
		}
		return crit.list();
	}

	@Override
	public ContaSms buscarPorUsername(String username) {
		Criteria crit = getSession().createCriteria(ContaSms.class);
		crit.add(Restrictions.eq("username", username).ignoreCase());
		return (ContaSms) crit.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<ContaSms> listarContasPorUsuario(Usuario usuario) {
		Criteria crit = getSession().createCriteria(ContaSms.class);
		crit.add(Restrictions.eq("ativo", Boolean.TRUE));
		crit.createAlias("usuarios", "usuario");
		crit.add(Restrictions.or(Restrictions.eq("criador.id", usuario.getId()),
				Restrictions.eq("usuario.id", usuario.getId())));
		crit.addOrder(Order.asc("nome"));
		return crit.list();
	}

}
